home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / forth / cforthu.arc / CFORTH.MSG < prev    next >
Encoding:
Text File  |  1989-10-11  |  4.3 KB  |  120 lines

  1. Category 1,  Topic 22
  2. Message 4         Mon Oct 09, 1989
  3. GARY-S                       at 14:01 EDT
  4.  
  5.  
  6.  From: marc@noe.UUCP (Marc de Groot)
  7.  
  8.  Subject: [4mRe: Alan Pratt's C-Forth
  9.  Date: 8 Oct 89 01:52:05 GMT
  10.  
  11.  In article <4825@cps3xx.UUCP> jhl@frith.egr.msu.edu () writes:
  12.  >I am using Alan Pratt's C-Forth under SCO XENIX 386 2.3.1 and have the
  13.  >following problem:
  14.  >
  15.  >In the editor, when I edit an existing block, mark it for updating,
  16.  >and flush it out to disk, exit forth, restart forth and look at the
  17.  >block file, the original block is unchanged and the block which results
  18.  >from my modifications appears at the end of the block file. 
  19.  
  20.  C-Forth contains significant errors, especially in the disk I/O code.
  21.  Try the following fixes.  I have not used the interpreter extensively
  22.  but these fixes seem to work on preliminary testing.
  23.  
  24.  The following piece of code contains a fix for the problem you
  25.  mentioned.  See the first comment below.  This code fragment
  26.  replaces getblockfile() in forth.c
  27.  
  28.  getblockfile()
  29.  {
  30.        /* M000 Changed the file mode from "a+" to "r+" on next line
  31.         * and added the call to fseek. Also added the declaration
  32.         * of ftell() and fopen().
  33.         */
  34.        long ftell(); /* M000 */
  35.        FILE *fopen(); /* M000 */
  36.  
  37.        if ((blockfile = fopen(bfilename, "r+")) == NULL) /* M000 */
  38.                errexit("Can't open blockfile \"%s\"\n", bfilename);
  39.        fseek(blockfile, 0L, 2); /* M000 */
  40.        bfilesize = ftell(blockfile);
  41.  
  42.        printf("Block file has %d blocks.\n",(int) (bfilesize/1024) - 1);
  43.  }
  44.  
  45.  In prims.c replace prslw() with the following code.  SLOWSTACK should not
  46.  be defined.  Cell should be typedef'd to int (or whatever is the correct
  47.  Forth cell size).  
  48.  
  49.  prslw()
  50.  {
  51.        unsigned cell buffer, addr; /* M002 */ /* M003 */
  52.  #ifdef SLOWSTACK /* M004 */
  53.        cell pop(); /* M003 */
  54.  #endif /* SLOWSTACK -- M004 */
  55.        cell flag; /* M003 */
  56.        int i, temp, unwrittenflag = FALSE; /* M000 */
  57.        long fpos, ftell();
  58.        char buf[1024];         /* holds data for xfer */
  59.  
  60.        flag = pop();
  61.        buffer = pop();
  62.        addr = pop();
  63.        fpos = (long) (buffer * 1024);
  64.  
  65.                                        /* extend if necessary */
  66.        if (fpos >= bfilesize) {
  67.            if (flag == 0) {            /* write */
  68.                printf("Extending block file to %ld bytes\n", fpos+1024);
  69. /* M00[K1 */
  70.                /* the "2" below is the fseek magic number for "beyond end" */
  71.                fseek(blockfile, (fpos+1024) - bfilesize, 2);
  72.                bfilesize = ftell(blockfile);
  73.            }
  74.            else {                      /* reading unwritten data */
  75.                unwrittenflag = TRUE;   /* will read all zeroes */
  76.            }
  77.        }
  78.        else {
  79.                /* note that "0" below is fseek magic number for "relative to
  80.                   beginning-of-file" */
  81.                fseek(blockfile, fpos, 0);      /* seek to destination */
  82.        }
  83.  
  84.        if (flag) {             /* read */
  85.            if (unwrittenflag) {        /* not written yet */
  86.                for (i=0; i<1024; i++) mem[addr++] = 0; /* "read" nulls */
  87.            }
  88.            else {                      /* does exist */
  89.                if ((temp = fread (buf, sizeof(char), 1024, blockfile)) 
  90.                                                                != 1024)
  91. {
  92.                        fprintf (stderr,
  93.                                "File read error %d reading buffer %d\n",
  94.                                        temp, buffer);
  95.                        errexit();
  96.                }
  97.                for (i=0; i<1024; i++) mem[addr++] = buf[i];
  98.            }
  99.        }
  100.        else {  /* write */
  101.                for (i=0; i<1024; i++) buf[i] = mem[addr++];
  102.                if ((temp = fwrite (buf, sizeof(char), 1024, blockfile))
  103.                                                                 != 1024)
  104. {
  105.                            fprintf(stderr,
  106.                                "File write error %d writing buffer %d\n",
  107.                                        temp, buffer);
  108.                            errexit();
  109.                }
  110.        }
  111.  }
  112.  
  113.  
  114.  -- 
  115.  Marc de Groot (KG6KF)                   These ARE my employer's opinions!
  116.  Noe Systems, San Francisco
  117.  UUCP: uunet!hoptoad!noe!marc
  118.  Internet: marc@kg6kf.AMPR.ORG
  119.  ------------
  120.